package com.wnhz.work.jy.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.solidfire.gson.Gson;
import com.wnhz.work.jy.R;
import com.wnhz.work.jy.activity.GoodDetailsMainActivity;
import com.wnhz.work.jy.adapter.EvaluateAdapter;
import com.wnhz.work.jy.bean.CommentsBean;
import com.wnhz.work.jy.bean.EvaluateBean;
import com.wnhz.work.jy.utils.ConfigUtil;
import com.wnhz.work.jy.utils.xutils3.MyCallBack;
import com.wnhz.work.jy.utils.xutils3.XUtil;
import com.wnhz.work.jy.view.XListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * A simple {@link Fragment} subclass.
 * -----评价
 */
public class EvaluateFragment extends Fragment implements XListView.IXListViewListener {
    private static final String TAG = "EvaluateFragment";
    private View view;
    private XListView lv_evaluate;

    private List<CommentsBean> evaluateList = new ArrayList<>();
    private EvaluateAdapter mAdapter;

    private View footView;

    private View include_occupied;
    private ImageView blank_img;
    private TextView blank_tv;
    private GoodDetailsMainActivity activity;
    private String is_last;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_evaluate, container, false);
        activity = (GoodDetailsMainActivity) getActivity();
        //initData();
        initView();
        return view;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        footView = LayoutInflater.from(activity).inflate(R.layout.foot_view,null);
        getData();
    }

    private void setBlank(){
        if (evaluateList.size() == 0){
            blank_tv.setText("暂无评论信息~");
            blank_img.setImageResource(R.mipmap.ic_youhuijuanweikong);
            include_occupied.setVisibility(View.VISIBLE);
        }else {
            include_occupied.setVisibility(View.GONE);
        }
    }
    private void initView() {
        include_occupied = view.findViewById(R.id.include_occupied);
        blank_tv = (TextView) view.findViewById(R.id.blank_tv);
        blank_img = (ImageView) view.findViewById(R.id.blank_img);
        lv_evaluate = (XListView) view.findViewById(R.id.lv_evaluate);

        mAdapter = new EvaluateAdapter(getActivity(), evaluateList);
        lv_evaluate.setAdapter(mAdapter);
        lv_evaluate.setPullRefreshEnable(false);
        lv_evaluate.setPullLoadEnable(true);
        lv_evaluate.setXListViewListener(this);
    }


    private int pageNum = 0;
    private void getData(){
        Map<String,Object> map = new HashMap<>();
        map.put("goodsId",activity.goodId);
        map.put("pageNum",pageNum+"");
        activity.showDialog();
        XUtil.Post(ConfigUtil.EVALUATPAGE,map,new MyCallBack<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e(TAG, "onSuccess: "+result );
                lv_evaluate.stopLoadMore();
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String re = jsonObject.optString("result");
                    if ("1".equals(re)){
                        JSONArray array = jsonObject.optJSONArray("comments");
                        if (array != null){
                            Gson gson = new Gson();
                            for (int i = 0; i < array.length(); i++) {
                                CommentsBean bean = new CommentsBean();
                                bean = gson.fromJson(array.optString(i),CommentsBean.class);
                                evaluateList.add(bean);
                            }
                        }

                    }
                    is_last = jsonObject.optString("is_last");
                    if ("1".equals(is_last)){
                        if (lv_evaluate.getFooterViewsCount() != 0){
                            lv_evaluate.removeFootView();
                        }
                        lv_evaluate.setPullLoadEnable(false);
                        lv_evaluate.addFooterView(footView);
                    }

                    mAdapter.notifyDataSetChanged();


                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
                activity.closeDialog();
                mAdapter.notifyDataSetChanged();
                setBlank();
            }
        });
    }

    @Override
    public void onRefresh() {

    }

    @Override
    public void onLoadMore() {
        pageNum++;
        getData();


    }
}
